package com.test.rabbitmq;

import com.rabbitmq.client.Channel;
import com.test.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * @author : caili
 * @date : 17:06 2022/11/30
 */
@Component
@Slf4j
public class DirectReceiver {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue("directQueue1"),
                    exchange = @Exchange(value = "directExchange"),//自定交换机名称和类型
                    key = "directRoutKey_one" //路由key
            )
    }, ackMode = "MANUAL")
    @RabbitHandler
    public void process(String pMessage, Message message, Channel channel) throws Exception {
        //消费者1接收的消息 路由模式消息[1、3、5、7.....]
        log.info("[路由/定向模式]消费者1接收生产中消息:{}", pMessage);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //设置手动应答 方便演示不做业务逻辑和异常处理了
        channel.basicAck(deliveryTag, false);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue("directQueue2"),
                    exchange = @Exchange(value = "directExchange"),//自定交换机名称和类型
                    key = "directRoutKey_two" //路由key
            )
    }, ackMode = "MANUAL")
    @RabbitHandler
    public void process2(String pMessage, Message message, Channel channel) throws Exception {
        //消费者1接收的消息 路由模式消息[2、4、6、8......]
        log.info("[路由/定向模式]消费者2接收生产中消息:{}", pMessage);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //设置手动应答 方便演示不做业务逻辑和异常处理了
        channel.basicAck(deliveryTag, false);
    }

}
